DSC 40B

Problems tagged with "asymptotic notation"

Problem #032

Tags: asymptotic notation

Let \(f(n) = 3n^2 \log n\). True or False: \(f(n) = O(n^3)\).

Solution

True.

Since \(\log n\) grows more slowly than \(n\), we have \(3n^2 \log n \leq 3n^2 \cdot n = 3n^3\) for all \(n \geq 1\), so \(f(n) = O(n^3)\). (In fact, \(f(n) = \Theta(n^2 \log n)\).)

Problem #033

Tags: asymptotic notation

Consider the function \(f(n) = 5n^2 - 100n + 100.\) Which of the following asymptotic bounds are true? Choose all that apply.

Solution

\(\Omega(n)\), \(\Theta(n^2)\), \(O(n^2)\), \(\Omega(n^2)\), and \(O(n^3)\).

Since \(f(n) = \Theta(n^2)\), it is \(O\) of anything that grows at least as fast as \(n^2\) and \(\Omega\) of anything that grows at most as fast as \(n^2\). It is not \(O(n)\) (and so not \(\Theta(n)\)), and it is not \(\Omega(n^3)\) (and so not \(\Theta(n^3)\)).

Problem #034

Tags: asymptotic notation

Let \(f(n) = \displaystyle\frac{n^3 - 2n^2 + 100}{n + 10}\)

True or False: \(f(n) = O(n^4)\)

Solution

True.

The leading term of the numerator is \(n^3\) and the leading term of the denominator is \(n\), so \(f(n) = \Theta(n^2)\). Anything that is \(\Theta(n^2)\) is also \(O(n^4)\), since \(O\) only gives an upper bound.

Problem #035

Tags: asymptotic notation

Consider the function defined below:

\[ f(n) = \begin{cases} 3n, & \text{if $n$ is even;}\\ 5n, & \text{if $n$ is odd.} \end{cases} \]

True or False: \(f(n) = \Theta(n)\).

Solution

True.

For every \(n\), \(3n \leq f(n) \leq 5n\), so \(f(n)\) is bounded above and below by constant multiples of \(n\). It doesn't matter that \(f\) jumps between two different constants.

Problem #036

Tags: asymptotic notation

Consider the function \(f(n) = \sin(12 n) \cdot \cos(n) + 2\). A plot of this function is shown below:

True or False: this function is \(\Theta(1)\).

Solution

True.

This function is upper bounded by 3 and lower bounded by 1 for all \(n\), and is therefore \(\Theta(1)\).

Problem #037

Tags: asymptotic notation

Consider the function \(f(n) = \sin(12n) \cdot \cos(n) + 2\). A plot of this function is shown below:

True or False: \(f(n) = O(n^3)\).

Solution

True.

We saw that this function is \(\Theta(1)\), but it is also correct to say that it is \(O(n^3)\), though this is not a tight upper bound.

Problem #038

Tags: asymptotic notation

Suppose Algorithm A takes \(\Theta(n^2)\) time, while Algorithm B takes \(\Theta(2^n)\) time. True or False: there could exist an input on which Algorithm B takes less time than Algorithm A.

Solution

True.

Asymptotic notation only describes how the time grows for large \(n\) and hides constants. For example, if A takes \(1000n^2\) steps and B takes \(2^n\) steps, then B is faster on every input of size \(n = 10\) (1024 steps vs. 100,000).

Problem #039 🌶️

Tags: asymptotic notation

Suppose algorithm A takes \(\Theta(n^2)\) time, while algorithm B takes \(\Theta(n^3)\) time.

True or False: there must exist an input of size 100 on which Algorithm A takes less time than Algorithm B.

Solution

False.

Algorithm A could have really large constants. For example, it could take \(1{,}000{,}000\, n^2\) seconds to run, while algorithm B takes \(0.0001\, n^3\) seconds to run. Algorithm B will still be better for \(n = 100\).

Problem #045

Tags: asymptotic notation

Consider the function \(f(n) = \frac{n^8 + 2^{3 + \log n} - \sqrt n}{20 n^5 - n^2}\).

True or False: \(f(n) = O(n^{4})\).

Solution

True.

Note that \(2^{3 + \log n} = 8 \cdot 2^{\log n} = 8n\), so the dominant term in the numerator is \(n^8\), while the dominant term in the denominator is \(20n^5\). Therefore \(f(n) = \Theta(n^3)\), which is \(O(n^4)\).

Problem #046

Tags: asymptotic notation

Consider the function \(f(n) = \frac{n^8 + 2^{3 + \log n} - \sqrt n}{20 n^5 - n^2}\).

True or False: \(f(n) = \Omega(n^2)\).

Solution

True.

Since \(2^{3 + \log n} = 8n\), the numerator is dominated by \(n^8\) and the denominator by \(20n^5\), so \(f(n) = \Theta(n^3)\). Anything that is \(\Theta(n^3)\) is also \(\Omega(n^2)\), since \(\Omega\) only gives a lower bound.

Problem #047

Tags: asymptotic notation

Let

\[ f(n) = \frac{n^3 - 2n + 100}{\sqrt n} \cdot \frac{n}{n^3 + n^2 + \sin n} \cdot n^2 \]

True or False: \(f(n) = \Theta(n^2)\).

Solution

False.

Keeping only the dominant terms, the first factor is \(\Theta(n^3 / \sqrt n) = \Theta(n^{2.5})\) and the second is \(\Theta(n / n^3) = \Theta(1/n^2)\). Multiplying by \(n^2\) gives \(f(n) = \Theta(n^{2.5})\), which is not \(\Theta(n^2)\) (it is not \(O(n^2)\)).

Problem #048 🌶️

Tags: asymptotic notation

Let

\[ f(n) = \frac{n^3 + 10 n}{\sqrt{n} - 100} \times \frac{ \log(n^2 + 10)}{n(n+1)} \]

Write \(f(n)\) in asymptotic notation in the simplest terms possible.

Solution

\(f(n) = \Theta(\sqrt n \log n)\).

We simplify each piece by keeping only its dominant term. In the first fraction, the numerator \(n^3 + 10n\) is \(\Theta(n^3)\) and the denominator \(\sqrt n - 100\) is \(\Theta(\sqrt n)\). In the second fraction, \(\log(n^2 + 10) = \Theta(\log n^2) = \Theta(2 \log n) = \Theta(\log n)\), and \(n(n+1) = \Theta(n^2)\). Putting them together:

\[ \begin{aligned} f(n) &= \Theta\biggl(\frac{n^3}{\sqrt n}\biggr) \times \Theta\biggl(\frac{\log n}{n^2}\biggr) \\ &= \Theta(n^{2.5}) \times \Theta\biggl(\frac{\log n}{n^2}\biggr) \\ &= \Theta(n^{0.5} \log n) = \Theta(\sqrt n \log n). \end{aligned} \]

Problem #049

Tags: asymptotic notation

Let

\[ f(n) = 5 n \log n + \frac{n^3 + 5}{n + 2 + |\sin \pi n|} + n \sqrt n \]

Write \(f\) in asymptotic notation in the simplest terms possible.

Solution

\(f(n) = \Theta(n^2)\).

Since \(0 \leq |\sin \pi n| \leq 1\), the denominator of the middle term is between \(n + 2\) and \(n + 3\), so the middle term is \(\Theta(n^3 / n) = \Theta(n^2)\). The other terms, \(5n \log n\) and \(n\sqrt n = n^{1.5}\), grow more slowly than \(n^2\), so the middle term dominates.

Problem #050

Tags: asymptotic notation

Let

\[ \begin{aligned} f_1(n) &= \frac{5n^3 + 3n^2 - 10n + 100}{n + 10}\\ f_2(n) &= 3n + \sin(\pi n)\\ f_3(n) &= \frac{2n^2 + \log n}{(n + 5)(n - 5)} \end{aligned} \]

and define \(f(n) = f_1(n) \times f_2(n) \times f_3(n)\). What is \(f(n)\), in asymptotic notation, in simplest terms possible?

Solution

\(\Theta(n^3)\).

We simplify each factor: \(f_1(n) = \Theta(n^3 / n) = \Theta(n^2)\); \(f_2(n) = \Theta(n)\), since \(\sin(\pi n)\) is between \(-1\) and \(1\); and \(f_3(n) = \Theta(n^2/n^2) = \Theta(1)\). So \(f(n) = \Theta(n^2) \times \Theta(n) \times \Theta(1) = \Theta(n^3)\).

Problem #051

Tags: asymptotic notation

Let \(f(n) = n \cdot (\sin{n} + 1 )\). A plot of this function is shown below.

True or False: \(f(n) = \Theta(n)\).

Solution

False.

Remember that for \(f(n)\) to be \(\Theta(n)\), it has to be both upper- and lower-bounded by some positive constant times \(n\). This function is upper bounded by \(O(n)\), but it doesn't have a lower bound of \(\Omega(n)\). Therefore, it can't be \(\Theta(n)\).

In other words, there is no positive constant \(c\) and no positive number \(N\) such that \(f(n) > c n\) for all \(n > N\).

If you had to describe this function in asymptotic notation, you could say that \(f(n) = O(n)\) (and that would be a tight upper bound), but there is no lower bound, since the function keeps coming back down to zero.

Problem #052

Tags: asymptotic notation

Consider the function \(f(n) = n \times (\sin(n) + 1)\). A plot of this function is shown below:

True or False: this function is \(O(1 + \sin n)\).

Solution

False.

\(f(n)\) grows arbitrarily large, while \(\sin n + 1\) is bounded above by 2.

More formally, there is no constant \(c\) such that \(c (1 + \sin n)\) upper bounds \(f(n)\) for all large \(n\).

Problem #053

Tags: asymptotic notation

Consider the function \(f(n) = n \times ( \sin (n) + 1)\). A plot of this function is shown below:

True or False: \(f(n) = O(n^3)\).

Solution

True.

Since \(\sin(n) + 1\) is always between 0 and 2, we have \(0 \leq f(n) \leq 2n\), so \(f(n) = O(n)\) and therefore also \(O(n^3)\). (The oscillation only prevents \(f\) from being \(\Omega(n)\); it doesn't affect the upper bound.)

Problem #054

Tags: asymptotic notation

Let \(f\) be the piecewise function defined as:

\[ f(n) = \begin{cases} 1, & \text{if $n$ is a multiple of 1 million},\\ n^2, &\text{otherwise}. \end{cases} \]

If you were to plot \(f\), the function would look like \(n^2\), but would have point discontinuities where it “jumps” back to 1 whenever \(n\) is a multiple of 1 million.

True or False: \(f(n) = \Theta(n^2)\).

Solution

False.

\(f(n)\) is upper bounded by \(O(n^2)\), but it doesn't have a lower bound of \(\Omega(n^2)\), which is necessary for it to be \(\Theta(n^2)\).

To see why, remember that for \(f(n)\) to be \(\Omega(n^2)\), there must be a positive constant \(c\) so that \(f(n)\) stays above \(c\cdot n^2\) for all \(n\) greater than some \(n_0\), meaning that it goes above \(c n^2\) and it stays above \(c n^2\) forever, after some point.

Pick whatever positive \(c\) you'd like—the function \(c\cdot n^2\) grows larger and larger as \(n\) increases, and eventually becomes larger than 1. But the function \(f(n)\) keeps dipping down to 1, meaning that it doesn't stay above \(c\cdot n^2\) for all \(n\) when \(n\) is large. Therefore, \(f(n)\) is not \(\Omega(n^2)\), and therefore also not \(\Theta(n^2)\).

Problem #057

Tags: asymptotic notation

Let \(f(n) = n^{ \frac{1}{\log_2(n)} } \times n.\) Give a tight asymptotic bound on \(f\) using \(\Theta\) notation, in the simplest form possible.

Hint: you might want to review your log properties. One useful one may be \(\log_b c = 1 / \log_c b\).

Solution

\(\Theta(n)\).

Using \(\frac{1}{\log_2 n} = \log_n 2\), we get \(n^{1/\log_2 n} = n^{\log_n 2} = 2\). So \(f(n) = 2n = \Theta(n)\).

Problem #058

Tags: asymptotic notation

Let \(f(n) = 12 \log_2(3^{(n^2 - 2n)} + 2^{(\log n)} - 10n^2 - \log_3 n).\) Give a tight asymptotic bound on \(f\) using \(\Theta\) notation, in the simplest form possible.

Solution

\(\Theta(n^2)\).

Inside the logarithm, the term \(3^{(n^2 - 2n)}\) dominates the others (which grow at most like \(n^2\)), so for large \(n\) the argument is between \(\frac{1}{2} \cdot 3^{(n^2 - 2n)}\) and \(2 \cdot 3^{(n^2 - 2n)}\). Taking \(\log_2\), \(f(n)\) is within a constant of \(12 (n^2 - 2n) \log_2 3\), which is \(\Theta(n^2)\).

Problem #059

Tags: asymptotic notation

Suppose \(f_1(n)\) is \(O(n^2)\) and \(\Omega(n)\). Also suppose that \(f_2(n) = \Theta(n^2)\).

Consider the function \(f(n) = f_1(n) + f_2(n)\). True or false: it must be the case that \(f(n) = \Theta(n^2)\).

Solution

True.

The upper bound: both \(f_1\) and \(f_2\) are \(O(n^2)\), so their sum is \(O(n^2)\). The lower bound: \(f_1\) is nonnegative (for large \(n\)), so \(f_1(n) + f_2(n) \geq f_2(n)\), which is \(\Omega(n^2)\).

Problem #060

Tags: asymptotic notation

Suppose \(f(n)\) is both \(O(n^2)\) and \(\Omega(n)\), and suppose \(g(n) = \Theta(n^3)\). What are the tightest possible asymptotic bounds that can be placed on \(f + g\)?

If the upper and lower bounds are the same, you may use \(\Theta\). If the upper and lower bounds are different, you should state them separately using \(O\) and \(\Omega\).

Solution

\(\Theta(n^3)\).

Since \(f\) is \(O(n^2)\), it grows more slowly than \(g = \Theta(n^3)\), so \(g\) dominates the sum: \(f + g \leq c n^2 + c' n^3 = O(n^3)\). And since \(f\) is positive, \(f + g \geq g = \Omega(n^3)\).

Problem #061

Tags: asymptotic notation

Suppose \(f(n)\) is both \(O(n^2)\) and \(\Omega(n)\), and suppose \(g(n) = \Theta(n^3)\). What are the tightest possible asymptotic bounds that can be placed on the product, \(f \times g\)?

If the upper and lower bounds are the same, you may use \(\Theta\); otherwise you should use \(O\) and \(\Omega\).

Solution

\(O(n^5)\) and \(\Omega(n^4)\).

Multiplying the bounds, \(f \times g\) is at most about \(n^2 \cdot n^3 = n^5\) and at least about \(n \cdot n^3 = n^4\). Both bounds are tight, since \(f\) could be exactly \(n^2\) or exactly \(n\), so we can't use \(\Theta\).

Problem #062

Tags: asymptotic notation

Suppose \(f_1(n)\) is \(O(n^3)\) and \(\Omega(n)\). Also suppose that \(f_2(n) = O(n^2)\) and \(\Omega(\sqrt n)\).

Consider the function \(f(n) = f_1(n) + f_2(n)\). True or false: it must be the case that \(f(n) = \Omega(n)\).

Solution

True.

Both functions are nonnegative (for large \(n\)), so \(f_1(n) + f_2(n) \geq f_1(n)\), and \(f_1(n) = \Omega(n)\). Adding a nonnegative function can only make the sum bigger, so it can't hurt a lower bound.

Problem #063 🌶️

Tags: asymptotic notation

Suppose that \(f_1(n)\) is \(O(n^3)\) and \(\Omega(n)\). Also suppose that \(f_2(n) = O(n^2)\) and \(\Omega(\sqrt n)\).

Consider the function \(g(n) = f_2(n) / f_1(n)\). Give the tightest possible upper bound on \(g(n)\).

Solution

\(O(n)\).

To find an upper bound on a fraction, we want the largest the numerator can be and the smallest the denominator can be. Since \(f_2(n) = O(n^2)\), the numerator is at most \(c_2 n^2\) for large \(n\). Since \(f_1(n) = \Omega(n)\), the denominator is at least \(c_1 n\). So

\[ g(n) = \frac{f_2(n)}{f_1(n)} \leq \frac{c_2 n^2}{c_1 n} = \frac{c_2}{c_1} n, \]

which means \(g(n) = O(n)\). Notice that the upper bound on \(g\) uses the lower bound on \(f_1\); the \(O(n^3)\) bound on \(f_1\) and the \(\Omega(\sqrt n)\) bound on \(f_2\) don't matter here.

This bound is the tightest possible, since \(f_2(n)\) could be exactly \(n^2\) and \(f_1(n)\) could be exactly \(n\), in which case \(g(n) = n\), which isn't \(O(n^{c})\) for any \(c < 1\).

Problem #064

Tags: asymptotic notation

Suppose \(f_1(n)\) is \(O(n^3)\) and \(\Omega(n^2)\). Also suppose that \(f_2(n)\) is \(O(n^4)\) and \(\Omega(n)\).

Consider the function \(f(n) = f_1(n) + f_2(n)\). True or false: it must be the case that \(f(n) = \Omega(n^2)\).

Solution

True.

Both functions are nonnegative (for large \(n\)), so \(f_1(n) + f_2(n) \geq f_1(n)\), and \(f_1(n) = \Omega(n^2)\). The weaker lower bound on \(f_2\) doesn't matter, since adding \(f_2\) can only make the sum larger.

Problem #065

Tags: asymptotic notation

Suppose that \(f_1(n)\) is \(O(n^3)\) and \(\Omega(n^2)\). Also suppose that \(f_2(n)\) is \(O(n^4)\) and \(\Omega(n)\).

Consider the function \(g(n) = f_2(n) / f_1(n)\). Give the tightest possible upper bound on \(g(n)\).

Solution

\(O(n^2)\).

\(g\) is largest when the numerator is as large as possible and the denominator as small as possible: \(f_2(n) \leq c n^4\) and \(f_1(n) \geq c' n^2\), so \(g(n) \leq \frac{c\, n^4}{c'\, n^2} = O(n^2)\). This is tight, since \(f_2\) could be exactly \(n^4\) and \(f_1\) exactly \(n^2\).

Problem #066

Tags: asymptotic notation

True or False. If \(f = \Omega(n^5)\) and \(f = O(n^7)\) then \(f\) must be either \(\Theta(n^5)\), or \(\Theta(n^6)\), or \(\Theta(n^7)\).

Solution

False.

Counterexample: \(f = n^{6.5}\). This is neither \(\Theta(n^5)\) nor \(\Theta(n^6)\) nor \(\Theta(n^7)\); it is \(\Theta(n^{6.5})\).

Problem #067

Tags: asymptotic notation

Suppose \(f_1(n) = O(g_1(n))\) and \(f_2(n) = \Omega(g_2(n))\). True or False: it is necessarily the case that \(f_1 + f_2 = O(g_1(n))\).

Solution

False.

We know nothing about how large \(f_2\) can be, so it can dominate the sum. For example, take \(f_1(n) = g_1(n) = g_2(n) = 1\) and \(f_2(n) = n^2\). Then \(f_2 = \Omega(1)\), but \(f_1(n) + f_2(n) = 1 + n^2\) is not \(O(1)\).

Problem #068 🌶️

Tags: asymptotic notation

Suppose \(f(n) = \Omega(n^3)\) and \(g(n) = \Omega(n)\).

True or False: it is necessarily the case that \(f/g = \Omega(n^2)\).

Solution

False.

Take \(f(n) = n^3\) and \(g(n) = n^3\). We can see from definition that \(f(n) = \Omega(n^3)\) and \(g(n) = \Omega(n)\). However,

\[ \frac{f}{g}(n) = 1 \neq \Omega(n^2). \]

The key takeaway for this question is, since \(g\) is in the denominator, we can make it grow arbitrarily fast, so that \(f/g\) does not meet the requirements that we want.

Problem #069

Tags: asymptotic notation

Suppose \(f_1(n)\) is \(O(n^2)\) and \(\Omega(n)\). Also suppose that \(f_2(n) = \Theta(n^2)\).

Consider the function \(g(n) = f_2(n) / f_1(n)\). True or false: it must be the case that \(g(n) = \Omega(n)\).

Solution

False.

\(f_1\) could be as large as \(n^2\). For example, if \(f_1(n) = f_2(n) = n^2\), then \(g(n) = 1\), which is not \(\Omega(n)\). (We only know \(g(n) = \Omega(1)\) and \(g(n) = O(n)\).)

Problem #070

Tags: asymptotic notation

True or False. If \(f_1 = \Theta(g_1(n))\) and \(f_2 = O(g_2(n))\) then \(\frac{f_1}{f_2} = \Theta(g_1 / g_2)\).

Solution

False.

Try breaking it with a counterexample. What if \(f_1 = n^3\), \(f_2 = n^2\), \(g_1 = n^3\) and \(g_2 = n^3\)? All of the conditions are satisfied, but \(f_1/f_2 = n\), while \(g_1/g_2 = 1\), so \(f_1 / f_2\) is not \(\Theta(g_1/g_2)\).

Problem #071

Tags: asymptotic notation

Suppose \(f_1(n) = O(g_1(n))\) and \(f_2(n) = O(g_2(n))\). Define \(f(n) = \min \{ f_1(n), f_2(n) \}\) and \(g(n) = \min \{ g_1(n), g_2(n) \}\). True or false: it is necessarily the case that \(f(n) = O(g(n))\).

Solution

True.

For large \(n\), \(f_1(n) \leq c_1 g_1(n)\) and \(f_2(n) \leq c_2 g_2(n)\). The minimum of \(f_1\) and \(f_2\) is at most each of them, so with \(c = \max\{c_1, c_2\}\) we get \(\min\{f_1(n), f_2(n)\} \leq c \, g_1(n)\) and \(\leq c \, g_2(n)\), i.e., \(f(n) \leq c \min\{g_1(n), g_2(n)\}\).

Problem #072

Tags: asymptotic notation

Suppose \(f_1(n) = \Omega(g_1(n))\) and \(f_2(n) = \Omega(g_2(n))\). Define \(f(n) = \min \{ f_1(n), f_2(n) \}\) and \(g(n) = \min \{ g_1(n), g_2(n)\}\).

True or false: it is necessarily the case that \(f(n) = \Omega(g(n))\).

Solution

True.

For large \(n\), \(f_1(n) \geq c_1 g_1(n)\) and \(f_2(n) \geq c_2 g_2(n)\). Letting \(c = \min\{c_1, c_2\}\), whichever of \(f_1(n)\) or \(f_2(n)\) is smaller is still at least \(c\) times the corresponding \(g\), which is at least \(c \min\{g_1(n), g_2(n)\}\). So \(f(n) \geq c \, g(n)\).

Problem #073 🌶️

Tags: asymptotic notation

Suppose \(f_1(n) = \Omega(g_1(n))\) and \(f_2(n) = \Omega(g_2(n))\). Define \(f(n) = \max \{ f_1(n), f_2(n) \}\) and \(g(n) = \max \{ g_1(n), g_2(n)\}\).

True or false: it is necessarily the case that \(f(n) = \Omega(g(n))\). In other words, it must be the case that \( \max \{ f_1(n), f_2(n) \} = \Omega ( \max\{ g_1(n), g_2(n) \} ). \)

Solution

True.

For large \(n\), \(f_1(n) \geq c_1 g_1(n)\) and \(f_2(n) \geq c_2 g_2(n)\). Letting \(c = \min\{c_1, c_2\}\), \(\max\{f_1(n), f_2(n)\}\) is at least both \(c\, g_1(n)\) and \(c\, g_2(n)\), so it is at least \(c \max\{g_1(n), g_2(n)\}\).